package com.zhongshengnetwork.rightbe.dbservice;

import com.zhongshengnetwork.rightbe.CustomApplication;
import com.zhongshengnetwork.rightbe.dbmodel.ShopCardbModel;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.util.List;

/**
 * Created by GordonLjx on 2016/10/8.
 */
public class ShopCardbService {
    //插入或者更新数据
    public static boolean saveOrUpdate(ShopCardbModel model){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            db.saveOrUpdate(model);
            return true;
        } catch (DbException e) {
            e.printStackTrace();
            return false;
        }
    }

    //根据id查找数据
    public static ShopCardbModel getdbDataById(String ids){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            ShopCardbModel model = db.findById(ShopCardbModel.class,ids);
            return model;
        } catch (DbException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * @return
     */
    //查找数据
    public static List<ShopCardbModel> getShopCardbModel(String userid){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            if (db.selector(ShopCardbModel.class).where("userid","==",userid) != null){
                List<ShopCardbModel> list = db.selector(ShopCardbModel.class).where("userid","==",userid).findAll();
                return list;
            }
            return null;
        } catch (DbException e) {
            e.printStackTrace();
            return null;
        }
    }

    //判断是否是一次性商品
    public static long isOneBuyGoods(String userid){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            if (db.selector(ShopCardbModel.class).where("userid","==",userid).and("type","==","2") != null){
                return db.selector(ShopCardbModel.class).where("userid","==",userid).and("type","==","2").count();
            }
            return 0;
        } catch (DbException e) {
            e.printStackTrace();
            return 0;
        }
    }

    //判断是否是积分商品
    public static long isJifenGoods(String userid){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            if (db.selector(ShopCardbModel.class).where("userid","==",userid).and("type","==","3") != null){
                return db.selector(ShopCardbModel.class).where("userid","==",userid).and("type","==","3").count();
            }
            return 0;
        } catch (DbException e) {
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * @return
     */
    //查找数据数量
    public static long getShopCarCount(String userid){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            if (db.selector(ShopCardbModel.class).where("userid","==",userid) != null)
              return db.selector(ShopCardbModel.class).where("userid","==",userid).count();
            return 0;
        } catch (DbException e) {
            e.printStackTrace();
            return 0;
        }
    }

    //删除数据
    public static boolean deleteShopCardbModel(String userid){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            db.delete(ShopCardbModel.class,db.selector(ShopCardbModel.class).where("userid","==",userid).getWhereBuilder());
            return true;
        } catch (DbException e) {
            e.printStackTrace();
            return false;
        }
    }

    //删除数据
    public static boolean deleteShopCardbModelByGoodsId(String goodsid){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            db.delete(ShopCardbModel.class,db.selector(ShopCardbModel.class).where("goodsid","==",goodsid).getWhereBuilder());
            return true;
        } catch (DbException e) {
            e.printStackTrace();
            return false;
        }
    }

    //删除数据
    public static boolean deleteShopCardbModelByType(String type){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            db.delete(ShopCardbModel.class,db.selector(ShopCardbModel.class).where("type","==",type).getWhereBuilder());
            return true;
        } catch (DbException e) {
            e.printStackTrace();
            return false;
        }
    }

    //删除数据
    public static boolean deleteShopCardbModelNotType(String type){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            db.delete(ShopCardbModel.class,db.selector(ShopCardbModel.class).where("type","!=",type).getWhereBuilder());
            return true;
        } catch (DbException e) {
            e.printStackTrace();
            return false;
        }
    }

    //删除数据
    public static boolean deleteShopCardbModelById(String id){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            db.deleteById(ShopCardbModel.class,id);
            return true;
        } catch (DbException e) {
            e.printStackTrace();
            return false;
        }
    }
}
